<?php

/**
 * @copyright : ienter asia Technologies
 * @author :  HungLV
 * @version    Release: V1.0
 * @Description : Class Home
 */
class Mod_home extends CI_Controller {
    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Load library Helper form,url, Model Mmod_user and load library session and setup language
     * @Input :         Null
     * @Output :        Null
     */
    
    

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');
        $this->load->model('mod_user/Mmod_user');
        $this->load->model('Mmod_home');                 
        
        $lang = $this->session->userdata('lang');
        if ($lang == "jp") {
            $this->session->set_userdata('lang', 'jp');
            $this->lang->load('jp', 'japanese');
            $this->config->set_item('language', 'japanese');
        }
        if ($lang == "en") {
            $this->session->set_userdata('lang', 'en');
            $this->lang->load('en', 'english');
            $this->config->set_item('language', 'english');
        }
        if ($lang == "") {
            $this->session->set_userdata('lang', 'jp');
            $this->lang->load('jp', 'japanese');
            $this->config->set_item('language', 'japanese');
        }
        /* Begin Quyet Add */
        $this->Mmod_home->setRangeYear();
        /* End Quyet Add */
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    check session UserName
     * @Input :         Null
     * @Output :        
     */

    function checkExitLogin() {
        if ($this->session->userdata('UserName')&&$this->session->userdata('CompanyCode')) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    show view home for soft
     * @Input :         Null
     * @Output :        
     */

    public function index() {
        // check exit login
        if ($this->checkExitLogin()) {
            redirect('/upload');
        } else {
            $data['title'] = $this->lang->line('Home_system');
            $data['show'] = "index";
            $data['data'] = "";
            $this->load->view('layout', $data);
        }
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Check login user name
     * @Input :         Null
     * @Output :        
     */

    function login() {
        if ($this->input->post('btnSubmit')) {
            $this->form_validation->set_error_delimiters('', '');
            $this->form_validation->set_rules('txtUserName', $this->lang->line('Home_UserName'), 'required|max_length[50]');
            $this->form_validation->set_rules('txtPassword', $this->lang->line('Home_Password'), 'required|min_length[8]|max_length[32]');
            $this->form_validation->set_rules('txtCompanyCode', $this->lang->line('Home_CompanyCode'), 'required|max_length[50]');
            if ($this->form_validation->run() == TRUE) {
                $username = $this->input->post('txtUserName');
                $password = $this->input->post('txtPassword');
                $CompanyCode = $this->input->post('txtCompanyCode');
                $langFirst = $this->input->post('cboLang');
                $data = $this->Mmod_user->getUserLogin($username, md5($password),$CompanyCode); 
                if (count($data) && ($data['UserName'] == $username) && ($data['Password'] == md5($password))&&$data['Status']==1) {
                    $this->session->set_userdata('UserName', $username);
                    $this->session->set_userdata('Level', $data['Level']);
                    $this->session->set_userdata('UserId', $data['UserId']);
                    $this->session->set_userdata('CompanyCode', $data['CompanyCode']);  
                    // Load Logo and header of customer.
                    $arrCompany=$this->Mmod_home->getCompany($data['CompanyCode']); 
                    $this->session->set_userdata('CompanyName', $arrCompany[0]->CompanyName);  
                    (empty($arrCompany[0]->Logo)) ? $this->session->set_userdata('Logo', 1) : $this->session->set_userdata('Logo', $arrCompany[0]->Logo);  
                    $this->session->set_userdata('PIC', $arrCompany[0]->PIC); 
                    // delete session lang
                    $this->session->unset_userdata('lang');
                    // set lang
                    if ($langFirst == "jp") {
                        $this->lang->load('jp', 'japanese');
                        $this->session->set_userdata('lang', 'jp');
                        $this->config->set_item('language', 'japanese');
                    }
                    if ($langFirst == "en") {
                        $this->lang->load('en', 'english');
                        $this->session->set_userdata('lang', 'en');
                        $this->config->set_item('language', 'english');
                    }
                    redirect('/upload');
                } else {
                    $data['title'] = $this->lang->line('Home_title');
                    $data['show'] = "index";
                    $data['message'] = $this->lang->line('Home_user_noexit');
                    $data['data'] = "";
                    $this->load->view('layout', $data);
                }
            } else {
                $data['title'] = $this->lang->line('Home_title');
                $data['show'] = "index";
                $data['data'] = "";
                $this->load->view('layout', $data);
            }
        } else {
            $data['title'] = $this->lang->line('Home_title');
            $data['show'] = "index";
            $data['data'] = "";
            $this->load->view('layout', $data);
        }
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Logout delete all session
     * @Input :         Null
     * @Output :        
     */

    function logout() {
        $this->session->unset_userdata('UserName');
        $this->session->unset_userdata('Level');
        $this->session->unset_userdata('UserId');
        $this->session->unset_userdata('CompanyName');
        $this->session->unset_userdata('Logo');
        $this->session->unset_userdata('PIC');
        redirect('/');
    }

    function jp() {
        // delete session lang
        $this->session->set_userdata('lang', 'jp');
        $this->config->set_item('language', 'japanese');
        $r = $this->session->userdata('lang');
        var_dump($r);
    }

    function en() {
        // delete session lang
        $this->session->set_userdata('lang', 'en');
        $this->config->set_item('language', 'english');
        $r = $this->session->userdata('lang');
        var_dump($r);
    }

    /**
     * @author HungLV        
     * @created:
     * @function resetpass
     * @This function Reset password user
     */
    function resetpass() {
        if ($this->checkExitLogin()) {
            redirect('/change-pass');
        } else {
            $temp = array();
            if ($this->input->post('btnSubmit')) {
                $temp = $this->Mmod_home->resetPassWord();
            }
            $info_home = $this->Mmod_home->getInfoResetPassWord();
            $data = array_merge($temp, $info_home);
            $this->load->view('layout', $data);
        }
    }

    /**
     * @author Thanh NV        
     * @created:2014/07/3
     * @function changepass      
     * @This function change password user
     */
    function changepass() {
        if ($this->checkExitLogin()) {
            $this->load->library('form_validation');
            $data = $this->Mmod_home->changePassword();
            $this->load->view('layout', $data);
        } else {
            redirect('/login');
        }
    }

}

?>